﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
namespace WebApp.admin.news
{
    public partial class article : BaseAdmin
    {
        tbArticle oArticle = new tbArticle();


        protected string atId = "00000000-0000-0000-0000-000000000000";



        // Methods
        private void ArticleBind()
        {
            try
            {
                oArticle = BaseFactory.theArticle.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (oArticle != null)
                {
                    this.txtArticleTilte.Value = oArticle.articleTilte;
                    base.ClientScript.RegisterStartupScript(base.GetType(), "st", "<script type=\"text/javascript\">selectIsExitItem(\"selectType\",\"" + oArticle.articleType + "\");</script>");
                    this.txtAuthor.Value = oArticle.author;
                    if (oArticle.isShow)
                    {
                        this.rdIsShow1.Checked = true;
                    }
                    else
                    {
                        this.rdIsShow0.Checked = true;
                    }
                    this.fckArticleContent.Value = oArticle.articleContent;
                    this.txtKeywords.Value = oArticle.keywords;
                    this.txtaDescription.Value = oArticle.description;
                }
            }
            catch
            {
            }
        }

        protected StringBuilder ArticleTypeBind()
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                this.RecursionArticeType(sb, "00000000-0000-0000-0000-000000000000");
            }
            catch
            {
            }
            return sb;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oArticle.articleTilte = this.txtArticleTilte.Value;
                this.oArticle.articleType = Convert.ToInt32(base.Request.Form["articleType"]);
                this.oArticle.author = this.txtAuthor.Value;
                this.oArticle.isShow = Gift.Utile.MathLibrary.ConvertNumToBool(base.Request.Form["isShow"]);
                this.oArticle.articleContent = this.fckArticleContent.Value;
                this.oArticle.keywords = this.txtKeywords.Value;
                this.oArticle.description = this.txtaDescription.Value;
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oArticle.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theArticle.Update(this.oArticle))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"articleList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    if (BaseFactory.theArticle.Add(this.oArticle) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"articleList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑文章");
                    return builder;
                }
                builder.Append("添加文章");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.ArticleManage);
            }

            if (!base.IsPostBack && ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null)))
            {

                this.atId = base.Request.QueryString["id"];
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.ArticleBind();
                }
                else
                {
                    this.rdIsShow1.Checked = true;
                }
            }
        }

        private void RecursionArticeType(StringBuilder sb, string parentType)
        {
            DataTable articleTypeByParentType = BaseFactory.theArticleType.GetArticleTypeByParentType(parentType);
            if (articleTypeByParentType.Rows.Count > 0)
            {
                foreach (DataRow row in articleTypeByParentType.Rows)
                {
                    string str = "";
                    int num = Convert.ToInt32(row["depth"]);
                    if (num > 0)
                    {
                        for (int i = 0; i < num; i++)
                        {
                            str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        str = str + "┣";
                    }
                    sb.AppendLine("<option value=\"" + row["typeIdentity"].ToString() + "\"  " + ((base.Request.QueryString["id"] == row["id"].ToString().ToUpper()) ? "selected='selected'" : "") + ">" + str + row["typeName"].ToString() + "</option>");
                    this.RecursionArticeType(sb, row["id"].ToString());
                }
            }
        }

    }
}